#!/bin/bash
# Install memcache and memcached for LNMP-2013
# 2013-12-02

# Remove libevent for system
rpm -e --nodeps libevent
rpm -e --nodeps libevent-devel

# Install libevent for Memcached
tar -zxf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/usr/local/libevent
make && make install
cd ..

# Install Memcache
tar -zxf memcache-3.0.8.tgz 
cd memcache-3.0.8
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
mv /usr/local/php/lib/php/extensions/no-debug*/memcache.so /usr/local/php/lib/php/extensions/
cd ..

# Configure Memcache for PHP
cat >> /usr/local/php/etc/php.d/memcache.ini << EOF
[Memcache]
extension = "/usr/local/php/lib/php/extensions/memcache.so"
EOF

# Install Memcached
tar -zxf memcached-1.4.15.tar.gz
cd memcached-1.4.15/
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make && make install
cd ..

# Configure Memcached
ln -s /usr/local/memcached/bin/memcached /usr/bin/memcached
cp memcached.init /etc/init.d/memcached
chmod +x /etc/init.d/memcached
chkconfig --add memcached && chkconfig memcached on
/etc/init.d/mamcached start

[ -f /etc/init.d/php-fpm ] && /etc/init.d/php-fpm restart
#[ -f /etc/init.d/httpd ] && /etc/init.d/httpd restart
